﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using FlotasDesktop;


namespace GestorDeFlotasDesktop.AbmUsuario
{
    public partial class BajaUsuario : Form
    {
        int id;
        String conexion;
        public BajaUsuario()
        {
            InitializeComponent();
            Utils utils = new Utils();
            conexion = utils.cargarProperties();
        }

        internal void mostrateConCampos(String id, String nombre, Boolean habilitiado)
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        this.id = Convert.ToInt32(id);
                        userTextBox.Text = nombre;
                        habilitadoCheckBox.Checked = habilitiado;
                        

                        List<string> idRoles = this.rolesDisponibles();

                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_cargarRolesHabilitados";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            DataTable dt = new DataTable();
                            int rowsRolesAsignados = adapter.Fill(dt);

                            foreach (DataRow dr in dt.Rows)
                            {
                                foreach (string ids in idRoles)
                                {
                                    if (dr[0].ToString().Equals(ids))
                                    {
                                        checkedListBox.CheckOnClick = true;

                                    }
                                }
                            }
                            checkedListBox.DataSource = dt;
                            checkedListBox.DisplayMember = "Nombre";
                            checkedListBox.ValueMember = "Id";
                            checkedListBox.CheckOnClick = true;


                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            this.Show();

        }

        private void borrarRolesUsuario()
        {
            using (SqlConnection connection2 = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command2 = new SqlCommand())
                    {

                        string sql = "delete from tequilaso.usuario_rol where id_usuario = " + this.id;
                        connection2.Open();
                        command2.Connection = connection2;
                        command2.CommandText = sql;
                        command2.CommandType = CommandType.Text;
                        command2.ExecuteReader();
                        connection2.Close();

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }


        private void Alta_Click(object sender, EventArgs e)
        {
            using (SqlConnection connection = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command = new SqlCommand())
                    {
                        this.borrarRolesUsuario();
                        connection.Open();
                        command.Connection = connection;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "TEQUILASO.sp_bajaUsuario";
                        command.Parameters.AddWithValue("@Id", this.id);
                        command.ExecuteNonQuery();
                        connection.Close();
                        this.Close();
                        MessageBox.Show("Baja realizada con éxito");

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private List<string> rolesDisponibles()
        {
            using (SqlConnection connection1 = new SqlConnection(conexion))
            {
                try
                {
                    using (SqlCommand command1 = new SqlCommand())
                    {
                        command1.Connection = connection1;
                        command1.CommandType = System.Data.CommandType.StoredProcedure;
                        command1.CommandText = "TEQUILASO.sp_buscarRolesPorUsuario";
                        command1.Parameters.AddWithValue("@Id_Usuario", id);
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command1))
                        {
                            DataTable dt = new DataTable();
                            int rows = adapter.Fill(dt);
                            List<string> idRoles = new List<string>();
                            foreach (DataRow dr in dt.Rows)
                            {
                                idRoles.Add(dr[0].ToString());

                            }
                            return idRoles;
                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                return new List<string>();
            }

        }


        private void BajaUsuario_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
